package other.edan.pass;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.api.annotations.RequestMethod;
import com.rkhd.platform.sdk.api.annotations.RestApi;
import com.rkhd.platform.sdk.api.annotations.RestMapping;
import com.rkhd.platform.sdk.api.annotations.RestQueryParam;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEvent;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventRequest;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventResponse;
import com.rkhd.platform.sdk.data.model.Account;
import com.rkhd.platform.sdk.data.model.DelApplyForDetails__c;
import com.rkhd.platform.sdk.data.model.DelApplyFor__c;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.exception.XsyHttpException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.OperateResult;
import other.edan.pass.utils.ComUtils;
import other.edan.pass.utils.HCXObjectService;
import other.edan.pass.utils.TfReturnMsgTools;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**功能描述：渠道客户审批通过后 ，生成CRM编码
 * @ClassName: ChannelAccountApproval
 * @Description:
 * @author 范伟绵
 * @date 2024年09月03日
 */

@RestApi(baseUrl = "/xsy/edan")
public class ChannelAccountApproval implements ApprovalEvent {
	private final static Logger LOGGER = LoggerFactory.getLogger();
	private final static String LOG_MSG_EX="渠道客户审批通过后 ，生成CRM编码:";

	
	public ApprovalEventResponse execute(ApprovalEventRequest request) throws ScriptBusinessException {
		Long id = request.getDataId();
		request.getEntityApiKey();
		LOGGER.info(LOG_MSG_EX + "开始，数据ID:"+id);
		
		ApprovalEventResponse response = new ApprovalEventResponse();
		try {
			response= rewriteCrmCode(id);
		} catch (Exception e) {
			response= TfReturnMsgTools.getApprResponse(false, LOG_MSG_EX + "失败.");
			LOGGER.error(LOG_MSG_EX +"发生异常"+e.getMessage());
			return response;
		}
		LOGGER.info(LOG_MSG_EX + "结束");
		return response;
	}
	
	@RestMapping(value = "/generateCRMCode", method = RequestMethod.POST)
	public String cancelDocument(@RestQueryParam(name="id") String id)  {
		LOGGER.info(LOG_MSG_EX+"执行了按钮事件");
		try{
			ApprovalEventResponse response= rewriteCrmCode(Long.valueOf(id));
			return TfReturnMsgTools.getJsonResult(response.getSuccess(),response.getMsg());
		}catch (Exception e){
			LOGGER.error("生成CRM编码:"+e.getMessage());
			return TfReturnMsgTools.getExceptionResult("生成CRM编码，请联系管理员！");
		}
	}

	private  ApprovalEventResponse  rewriteCrmCode(Long id) throws IOException {
		Account account=new Account();
		account.setId(id);
		account=HCXObjectService.get(account);

		if (!account.getEntityType().equals(3314472397658728L)){
			//非渠道客户
			return  TfReturnMsgTools.getApprResponse(false,"非渠道客户，无需生成CRM编码");
		}

		String sql="select id,crmCodeOnly__c from account WHERE crmCodeOnly__c > 0  order by crmCodeOnly__c desc";
		Account bigAccount=HCXObjectService.queryOne(sql);
		String code="1.DM"+String.format("%06d",bigAccount.getCrmCodeOnly__c().longValue()+1);

		Account updateAccount=new Account();
		updateAccount.setId(id);
		updateAccount.setCrmCode__c(code);
		if(null==account.getK3Code__c() || account.getK3Code__c().isEmpty()){
			updateAccount.setK3Code__c(code);
		}

		LOGGER.info(updateAccount.toString());
		OperateResult result=HCXObjectService.update(updateAccount);

		return TfReturnMsgTools.getApprResponse(result.getSuccess(), result.getErrorMessage());
	}

	public static void main(String[] args) throws IOException, ApiEntityServiceException, XsyHttpException {
		//String sql="select id,crmCodeOnly__c from account WHERE crmCodeOnly__c>0  order by crmCodeOnly__c desc";
		//Account bigAccount=HCXObjectService.queryOne(sql);
		//String code="1.DM"+String.format("%06d",bigAccount.getCrmCodeOnly__c().longValue()+1);
		//System.err.println(code);

        ChannelAccountApproval channelAccountApproval = new ChannelAccountApproval();
        channelAccountApproval.rewriteCrmCode(3716106282621517L);

        //Account account=new Account();
		//account.setId(3740225317608091L);
		////account.setCrmCode__c("1.DM002901");
        //account.setK3Code__c("1.DM003286");
		//HCXObjectService.update(account);
    }
}